<?php
// --------------------------------------------------------
// Tag handler class base ver 1.01
// included call tags utiltiy
// --------------------------------------------------------

if (!defined('XOOPS_ROOT_PATH')) exit();

class ScuttleTagsObject extends XoopsSimpleObject
{
	function ScuttleTagsObject()
	{
		$this->initVar('id', XOBJ_DTYPE_INT, '', true);
		$this->initVar('bId', XOBJ_DTYPE_INT, '0', true);
		$this->initVar('tag', XOBJ_DTYPE_STRING, '', true, 100);

		$this->initVar('new_tag' , XOBJ_DTYPE_STRING , '' , false ,100);
	}
}

class ScuttleTagsHandler extends XoopsObjectGenericHandler
{
	var $mClass = 'ScuttleTagsObject';
	var $mTable = 'scuttle_tags';
	var $mTag = 'tag';
	var $mIndexkey = 'id';
	var $mPrimary = 'bId';
	var $mHandleDb = 'tags';

	function adminTagList($sqlLimit=5, $sqlStart=0, $getSort='tagCount', $getOrder='DESC') {
		$retArray = array();
		$sql = sprintf('SELECT `%s`, COUNT(`%s`) AS tagCount FROM `%s` GROUP BY `%s` ORDER BY `%s` %s', $this->mTag, $this->mIndexkey, $this->mTable, $this->mTag, $getSort ,$getOrder );
		$result = $this->db->query($sql, $sqlLimit, $sqlStart) ;
		while (list($tagWord, $tagCount) = $this->db->fetchRow($result)) {
			array_push($retArray, array(
				'tag_word'  => $tagWord,
				'tag_count' => $tagCount,
			));
		}
		return $retArray;
	}

	function adminCountTagWords() {
		$sql = sprintf('SELECT COUNT(`%s`) AS tagCount FROM `%s` GROUP BY `%s`', $this->mIndexkey, $this->mTable, $this->mTag);
		$result = $this->db->query($sql);
		return $this->db->getRowsNum($result) ;
	}

	function getTagList( $setid = NULL) {
		$entryTags = ''; 
		$tagHandler = xoops_getmodulehandler($this->mHandleDb);
		$mCriteria = new CriteriaCompo();
		$mCriteria->add(new Criteria($this->mPrimary, $setid));
		$mCriteria->setSort($this->mTag);
		$mObject = $tagHandler->getObjects($mCriteria);
		foreach ($mObject as $key => $val) {
			$entryTags .= $val->getShow($this->mTag). ', ';
		}
		return $entryTags ;
	}

	function getEntoryTagArray( $setid = NULL) {
		$entryTags = array(); 
		$tagHandler = xoops_getmodulehandler($this->mHandleDb);
		$mCriteria = new CriteriaCompo();
		$mCriteria->add(new Criteria($this->mPrimary, $setid));
		$mCriteria->setSort($this->mTag);
		$mObject = $tagHandler->getObjects($mCriteria);
		foreach ($mObject as $key => $val) {
			$entryTags[] = $val->getShow($this->mTag);
		}
		return $entryTags ;
	}

	function renameTag($oldTag='', $newTag='') {
		if (is_null($oldTag) || is_null($newTag)) {
			return false;
		}
		$sql = sprintf("UPDATE `%s` SET `%s` = '%s' WHERE `%s`=%s ", $this->mTable, $this->mTag, $newTag, $this->mTag, $oldTag );
		return $this->db->query($sql);
	}

	function deleteTagId($delTagId = NULL) {
		$sql = sprintf("DELETE FROM `%s` WHERE `%s`=%s", $this->mTable, $this->mPrimary, $delTagId );
		return $this->db->queryF($sql);
	}


	function deleteTag($delTag = NULL) {
		$sql = sprintf("DELETE FROM `%s` WHERE `%s`='%s'", $this->mTable, $this->mTag, $delTag );
		return $this->db->queryF($sql);
	}

	function deleteTagArray($tagArray = NULL, $tags = NULL) {
		$tags_count = count($tags);
		$delPram = '';
		for ($i = 0; $i < $tags_count; $i++) {
			$delPram .= ' AND `'.$this->mTag. '` != "'. $tags[$i].'"' ;
		}
		$sql = sprintf("DELETE FROM `%s` WHERE `%s`='%s' '%s'", $this->mTable, $this->mPrimary, $tagArray, $delPram );
		return $this->db->query($sql);
	}

	function saveTags($setid =0 , $tagText='',$tagToLower = true)
	{
		$tagText = trim($tagText);
		if (is_null($tagText)) {
			return false;
		}
		if (!$setid > 0) {
			return false;
		}

		// TODO
		// バリテーションにタグの単体の長さのチェックを行う
		// タグの用語の長さは１００文字までにする

		$tagHandler = xoops_getmodulehandler($this->mHandleDb);
		
		$tempArray = array();    // temp array
		$deleteTagArray = array();
		$addTagArray = array();

		// tag text change to array
		$tagArray = explode(',', $tagText);
		$tags_count = count($tagArray);
		for ($i = 0; $i < $tags_count; $i++) {
			$tag = trim($tagArray[$i]);
			// tag word to lower
			if ($tagToLower) {
				$tag = strtolower($tag);
			}
			// delete spase
			if ($tag<>"") {
				$tempArray[] = $tag ;
			}
		}
		// tags array
		$tagArray = $tempArray;
		unset($tempArray);

		// delete duplicate tag
		$tags_count = count($tagArray);
		if ($tags_count > 0) {
			$tempArray = array_unique($tagArray);
			$tagArray = array_values($tempArray);
		}
		// temp array is reset
		unset($tempArray);

		// useing tag list
		$mCriteria = new CriteriaCompo();
		$mCriteria->add(new Criteria($this->mPrimary, $setid));
		$mCriteria->setSort($this->mTag);
		$mObjects = $tagHandler->getObjects($mCriteria);

		// check new tags & delete tags
		if ($mObjects == null) {
			$addTagArray = $tagArray;
		} else {
			foreach ($mObjects as $key => $val) {
				$tempArray[] = $val->getShow($this->mTag);
			}
			$deleteTagArray = array_diff($tempArray, $tagArray); 
			$addTagArray = array_diff($tagArray, $tempArray); 
		}

		// delete tags
		if (count($deleteTagArray) > 0) {
			foreach ($deleteTagArray as $key => $val) {
				$sql = sprintf("DELETE FROM `%s` WHERE `%s`=%u AND `%s` LIKE '%s'", $this->mTable, $this->mPrimary, $setid, $this->mTag, $val );
				$this->db->query($sql);
			}
		}

		// add tags
		if (count($addTagArray) > 0) {
			foreach ($addTagArray as $key => $val) {
				$sql = sprintf("INSERT INTO `%s` (%s, %s, %s) VALUES (%u, %u, '%s')", $this->mTable, $this->mIndexkey, $this->mPrimary, $this->mTag, NULL, $setid, $val );
				$result = $this->db->query($sql);
			}
		}
		return;
	}

}
?>